package com.domor.service.system;

import com.alibaba.fastjson.JSON;
import com.domor.dao.system.RoleDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleService {

    @Autowired
    private RoleDao dao;

    public Map<String,Object> getById(int id) {
        return dao.getById(id);
    }
    public List<Map<String, Object>> getRoles() {
        return dao.getRoles();
    }

    public List<Map<String, Object>> query(Map<String, Object> params) {
        return dao.query(params);
    }

    public void insert(Map<String, Object> params){
        dao.insert(params);
    }

    public void update(Map<String, Object> params){
        dao.update(params);
    }

    public void delete(Map<String, Object> params){
        dao.delete(params);
    }

    public List<Integer> getRights(int roleId) {
        return dao.getRights(roleId);
    }

    @Transactional
    public void saveRights(int roleId, String menus) {
        dao.deleteRights(roleId);
        List<Integer> ms = JSON.parseArray(menus, Integer.class);
        for (Integer m: ms) {
            Map<String, Object> rights = new HashMap<>();
            rights.put("menuId", m);
            rights.put("roleId", roleId);
            dao.saveRights(rights);
        }
    }

}
